/*
 * Copyright (C) 2011 Fabio Hufschmid at <hufsf1@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.core.io.tts.bloomfilter;

import android.content.Context;
import android.content.res.AssetManager;
import com.skjegstad.utils.BloomFilter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.Properties;

/**
 * Used to import the BitSet for the bloomfilter
 * @author Fabio Hufschmid at <hufsf1@bfh.ch>
 */
public class UtilsBloomfilter {
    private Byte mb_data;
    private List<Byte> mbal;
   
    /**This mehtod helps to import a BitSet from a file which were exported with the methode
     * exportBloomfilterToFile
     * 
     * @param inPathToFile  path to the file which contains the BitSet
     * @return              return the BitSet
     * @throws IOException 
     */
    public BitSet importBitSetToBloomfilter(String inPathToFile) throws IOException
    {
        mbal = new ArrayList<Byte>();
        try {
          // Wrap the FileInputStream with a DataInputStream
          FileInputStream file_input = new FileInputStream (inPathToFile);
          DataInputStream data_in    = new DataInputStream (file_input );      
          while(true) {
            try {
                    mb_data = data_in.readByte();
                    mbal.add(mb_data);
                } catch(EOFException eof) {
                    System.out.println("End of File");
                    data_in.close();
                    break;
                }
            }
        } catch  (IOException e) {
           System.out.println ( "IO Exception =: " + e );
        }
        byte[] inba = new byte[mbal.size()];
        for ( int i=0; i<inba.length; i++) 
            inba[i]=mbal.get(i);
        
        //System.out.println("In Data" );
        for (int i=0; i < inba.length; i++) {
              //System.out.print (""+inba[i] );
          }
       return fromByteArray(inba);
    }

    /**Convert a byte array to a BitSet
     * 
     * @param bytes byte array to convert
     * @return      converted BitSet 
     */
    public BitSet fromByteArray(byte[] bytes) {
        BitSet bits = new BitSet();
        for (int i = 0; i < bytes.length * 8; i++) {
          if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) != 0) {
            bits.set(i);
            //System.out.println("Added to BitSet: "+i);
            
          }
        }
        System.out.println("Imported BitSet: "+bits);
        return bits;
    }
    
    /**This mehtod helps to export a BitSet from a bloomfilter to file which can only imported with the methode
     * importBitSetToBloomfilter
     * 
     * @param inPath    Where the file is to save
     * @param inBF      defines the bloomfilter to export his BitSet
     */
    public void exportBloomfilterToFile(String inPath,BloomFilter<String> inBF)
    {
        //export bloomfilter to file
        // Now write the data array to the file.
        try {
          // Create an output stream to the file.
          FileOutputStream file_output = new FileOutputStream (inPath);
          // Wrap the FileOutputStream with a DataOutputStream
          DataOutputStream data_out = new DataOutputStream (file_output);

          // Write the data to the file in an integer/double pair
          byte [] b_data = null;
          BitSet BS=inBF.getBitSet();
          System.out.println("Exported BitSet: "+BS);
          //b_data = inBF.getBitSet().toByteArray();
          b_data =toByteArray(inBF.getBitSet());
          //System.out.println ("Export Data");
          for (int i=0; i < b_data.length; i++) {
              //System.out.print (""+b_data[i] );
              data_out.writeByte(b_data[i]);
          }
          System.out.println ("Size of outbytearray"+b_data.length);
          // Close file when finished with it..
          file_output.close ();
        }
        catch (IOException e) {
           System.out.println ("IO exception = " + e );
        }      
    }
    
    /**Convert a BitSet to a byte array 
     * 
     * @param bits  BitSet to convert
     * @return      converted byte array
     */
    public byte[] toByteArray(BitSet bits) {
        byte[] bytes = new byte[bits.length()/8+1];
        for (int i=0; i<bits.length(); i++) {
            if (bits.get(i)) {
                bytes[bytes.length-i/8-1] |= 1<<(i%8);
            }
        }
        return bytes;
    }
    
    public Properties readIniFile(String Path) {
        Properties pro =null;
        try {
            pro = new Properties();
            pro.load(new FileInputStream(Path)); 
        }
        catch(Exception ex) {
        
            System.out.println(ex.getMessage());
        }
        
        return pro;
    }
}
